<?php
namespace app\common\service\WebSocket\SocketService; 

use app\Normal;
use app\BaseService;
use app\model\BbcModel\Commission;

class BusinessFinancialSocketService extends BaseService
{
    public $socket_connector;

    public $fd;

    public $host;
    public $pageLimit      = 2000;
    
    public function orderExport($parameters = []) {
        ini_set('memory_limit', '3096M');
        $this->socket_connector = $parameters['socket'];
        $this->fd               = $parameters['fd'];
        $this->host             = $parameters['host'];
        $data                   = $parameters['requestData'];
        if(empty($data['cb_id'])){
            return Normal::returnWsStatus(Normal::WS_ERROR,"缺少参数cb_id");
        }
        $where=[
            ['cb_id','=',$data['cb_id']],
            ['status','=',1]
        ];
        //子订单号
        if(!empty($data['sn'])){
            $where[]=['sn','like','%'.$data['sn'].'%'];
        }
        //下单时间
        if(!empty($data['cd_start'])){
            $where[]=['md','>=',strtotime($data['cd_start'])];
        }
        if(!empty($data['cd_end'])){
            $where[]=['md','<=',strtotime($data['cd_end'])];
        }
        if(!empty($data['order_type'])){
            $where[]=['order_type','=',$data['order_type']];
        }
        $total_count=Commission::instance()
            ->where($where)
            ->group('order_id')
            ->count();
        $total_page  = ceil($total_count / $this->pageLimit);
        $importData  = [];
        for ($page = 0; $page < $total_page; $page++) {
            $list=Commission::instance()
                ->field('id,sn,sum(shop_money) as money,md,order_type')
                ->where($where)
                ->group('order_id')
                ->order('id desc')
                ->limit($page * $this->pageLimit, $this->pageLimit)
                ->select()
                ->toArray();
            if ($list) {
                $tmpData = [];
                foreach ($list as $v) {
                    $tmpData[] = [
                        $v['id'],
                        Commission::instance()->order_type[$v['order_type']],
                        $v['sn'],
                        $v['money'],
                        date('Y-m-d H:i:s',$v['md'])
                    ];
                }
                $importData = array_merge($importData, $tmpData);
            }

            if ($page == $total_page - 1) {
                $percent = 98.00; //预留给写文件使用
            } else {
                $percent = bcdiv($page, $total_page - 1, 4);
                $percent = bcmul($percent, 100, 2);
            }

            if ($this->socket_connector) {
                $send = Normal::returnWsStatus(Normal::WS_START, "进行中", [
                    'percent' => $percent,
                ]);
                $send = json_encode($send, JSON_UNESCAPED_UNICODE);
                $this->socket_connector->push($this->fd, $send);
            }
        }
        //数据准备完成开始写入excel本地数据
        $send = Normal::returnWsStatus(Normal::WS_START, "进行中", [
            'percent' => 99.00,
        ]);
        $send = json_encode($send, JSON_UNESCAPED_UNICODE);
        $this->socket_connector->push($this->fd, $send);

        $fileName     = "渠道分佣流水导出";
        $headArr      = $this->getColumns();
        $data         = $importData;
        $download_url = xlsxFileLocalStorage($headArr, $data, $fileName, $this->host);
        return Normal::returnWsStatus(Normal::WS_END, "已完成", [
            'percent'      => 100.00,
            'download_url' => $download_url,
            'name'=>'渠道分佣流水导出'
        ]);
    }
     //excel头部
    public function getColumns() {
        return [
            'ID编号',
            '资金用途',
            '订单编号',
            '返佣金额',
            '创建时间',
        ];
    }
}
